#include "stdafx.h"
//-------------------------------------------------------------------------------------------------
//ChildFrm.cpp : implementation of the CChildFrame class       
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk July 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------

#include "ArchiveProcGUI.h"
#include "ArchiveProcGUIDoc.h"
#include "MainFrm.h"
#include "ImageForm.h"

#include "ChildFrm.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CChildFrame

IMPLEMENT_DYNCREATE(CChildFrame, CMDIChildWndEx)

BEGIN_MESSAGE_MAP(CChildFrame, CMDIChildWndEx)
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVE, &CChildFrame::OnUpdateFileSave)
	ON_COMMAND_RANGE(IDS_SELECTTYPE, IDS_ENDTOOLTYPES-1,  OnSelectDynamicMenus)			
	ON_COMMAND(ID_EDIT_ERASESELIMAGEOBJECT, &CChildFrame::OnEditEraseSelImageObject)
	ON_UPDATE_COMMAND_UI(ID_EDIT_ERASESELIMAGEOBJECT, &CChildFrame::OnUpdateEditEraseSelImageObject)
END_MESSAGE_MAP()

// CChildFrame construction/destruction

CChildFrame::CChildFrame()
{
	// TODO: add member initialization code here
}

CChildFrame::~CChildFrame()
{
}


BOOL CChildFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying the CREATESTRUCT cs
	if( !CMDIChildWndEx::PreCreateWindow(cs) )
		return FALSE;

	return TRUE;
}

// CChildFrame diagnostics

#ifdef _DEBUG
void CChildFrame::AssertValid() const
{
	CMDIChildWndEx::AssertValid();
}

void CChildFrame::Dump(CDumpContext& dc) const
{
	CMDIChildWndEx::Dump(dc);
}
#endif //_DEBUG

// CChildFrame message handlers




BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	// TODO: Add your specialized code here and/or call the base class

	// create a splitter with 1 row, 2 columns
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
	{
		TRACE0("Failed to CreateStaticSplitter\n");
		return FALSE;
	}

	// add the first splitter pane - the default view in column 0
	if (!m_wndSplitter.CreateView(0, 0,
		pContext->m_pNewViewClass, CSize(130, 50), pContext))
	{
		TRACE0("Failed to create first pane\n");
		return FALSE;
	}

	// add the second splitter pane - an input view in column 1
	if (!m_wndSplitter.CreateView(0, 1,
		RUNTIME_CLASS(CImageForm), CSize(0, 0), pContext))
	{
		TRACE0("Failed to create second pane\n");
		return FALSE;
	}

	// activate the input view
	SelectTreeView();

	return TRUE;
}



void CChildFrame::OnUpdateFileSave(CCmdUI *pCmdUI)
{
	CImageForm *pForm = dynamic_cast<CImageForm*>(m_wndSplitter.GetPane(0,1));
	if (pForm)
	{
		CArchiveProcGUIDoc *pDoc = dynamic_cast<CArchiveProcGUIDoc *>(pForm->GetDocument());
		if (pDoc)
			pCmdUI->Enable(pDoc->IsModified());
		else
			pCmdUI->Enable(false);
	}
	else
		pCmdUI->Enable(false);
}

void CChildFrame::OnEditEraseSelImageObject()
{
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CChildFrame::OnEditEraseSelImageObject()", "user selected Edit | Erase");
#endif
//!!!!!!!!!!!!!!!

	CArchiveProcGUIDoc *pDoc = CArchiveProcGUIDoc::GetActiveDoc();
	if ((pDoc != '\0') && (pDoc->GetSelImageProperties() != '\0'))
	{
		pDoc->DoUpdateAllViews((CWnd*)m_wndSplitter.GetPane(0,0), CArchiveProcGUIDoc::UpdateAllViewsSave, NULL);
		pDoc->GetSelImageProperties()->DeleteSelImageObject(pDoc->GetSelImageObjectTypeTool());
		pDoc->DoUpdateAllViews((CWnd*)m_wndSplitter.GetPane(0,0), CArchiveProcGUIDoc::UpdateAllViewsDeleteImageObject, NULL);
	}
}


void CChildFrame::OnUpdateEditEraseSelImageObject(CCmdUI *pCmdUI)
{
	BOOL sel = FALSE;
	CArchiveProcGUIDoc *pDoc = CArchiveProcGUIDoc::GetActiveDoc();
	if ((pDoc != '\0') && (pDoc->GetSelImageProperties() != '\0'))
		 sel = (pDoc->GetSelImageProperties()->GetSelImageObject() != '\0') ? TRUE: FALSE;
	pCmdUI->Enable(sel);
}

void  CChildFrame::OnSelectDynamicMenus (UINT nID)
{
//!!!!!!!!!!!!!!!
#ifdef _DEBUG
	MxUtils::OutputDebugMsg("CChildFrame::OnSelectDynamicMenus()", "user selected dynamic menu item %d", nID);
#endif
//!!!!!!!!!!!!!!!

	CDocument *pDocument = GetActiveDocument();
	CArchiveProcGUIDoc *pDoc = dynamic_cast<CArchiveProcGUIDoc *>(pDocument);
	if (pDoc)
	{
		pDoc->DoUpdateAllViews((CWnd*)m_wndSplitter.GetPane(0,0), CArchiveProcGUIDoc::UpdateAllViewsSave, NULL);
		pDoc->SelectDymanicMenu(nID);
	}
}

void  CChildFrame::SelectTreeView()
{
	SetActiveView((CView*)m_wndSplitter.GetPane(0,0));
}




//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------





